<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2017-11-11 sáb 12:27 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Grep</title>
<meta name="generator" content="Org mode" />
<style type="text/css">
 <!--/*--><![CDATA[/*><!--*/
  .title  { text-align: center;
             margin-bottom: .2em; }
  .subtitle { text-align: center;
              font-size: medium;
              font-weight: bold;
              margin-top:0; }
  .todo   { font-family: monospace; color: red; }
  .done   { font-family: monospace; color: green; }
  .priority { font-family: monospace; color: orange; }
  .tag    { background-color: #eee; font-family: monospace;
            padding: 2px; font-size: 80%; font-weight: normal; }
  .timestamp { color: #bebebe; }
  .timestamp-kwd { color: #5f9ea0; }
  .org-right  { margin-left: auto; margin-right: 0px;  text-align: right; }
  .org-left   { margin-left: 0px;  margin-right: auto; text-align: left; }
  .org-center { margin-left: auto; margin-right: auto; text-align: center; }
  .underline { text-decoration: underline; }
  #postamble p, #preamble p { font-size: 90%; margin: .2em; }
  p.verse { margin-left: 3%; }
  pre {
    border: 1px solid #ccc;
    box-shadow: 3px 3px 3px #eee;
    padding: 8pt;
    font-family: monospace;
    overflow: auto;
    margin: 1.2em;
  }
  pre.src {
    position: relative;
    overflow: visible;
    padding-top: 1.2em;
  }
  pre.src:before {
    display: none;
    position: absolute;
    background-color: white;
    top: -10px;
    right: 10px;
    padding: 3px;
    border: 1px solid black;
  }
  pre.src:hover:before { display: inline;}
  /* Languages per Org manual */
  pre.src-asymptote:before { content: 'Asymptote'; }
  pre.src-awk:before { content: 'Awk'; }
  pre.src-C:before { content: 'C'; }
  /* pre.src-C++ doesn't work in CSS */
  pre.src-clojure:before { content: 'Clojure'; }
  pre.src-css:before { content: 'CSS'; }
  pre.src-D:before { content: 'D'; }
  pre.src-ditaa:before { content: 'ditaa'; }
  pre.src-dot:before { content: 'Graphviz'; }
  pre.src-calc:before { content: 'Emacs Calc'; }
  pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  pre.src-fortran:before { content: 'Fortran'; }
  pre.src-gnuplot:before { content: 'gnuplot'; }
  pre.src-haskell:before { content: 'Haskell'; }
  pre.src-hledger:before { content: 'hledger'; }
  pre.src-java:before { content: 'Java'; }
  pre.src-js:before { content: 'Javascript'; }
  pre.src-latex:before { content: 'LaTeX'; }
  pre.src-ledger:before { content: 'Ledger'; }
  pre.src-lisp:before { content: 'Lisp'; }
  pre.src-lilypond:before { content: 'Lilypond'; }
  pre.src-lua:before { content: 'Lua'; }
  pre.src-matlab:before { content: 'MATLAB'; }
  pre.src-mscgen:before { content: 'Mscgen'; }
  pre.src-ocaml:before { content: 'Objective Caml'; }
  pre.src-octave:before { content: 'Octave'; }
  pre.src-org:before { content: 'Org mode'; }
  pre.src-oz:before { content: 'OZ'; }
  pre.src-plantuml:before { content: 'Plantuml'; }
  pre.src-processing:before { content: 'Processing.js'; }
  pre.src-python:before { content: 'Python'; }
  pre.src-R:before { content: 'R'; }
  pre.src-ruby:before { content: 'Ruby'; }
  pre.src-sass:before { content: 'Sass'; }
  pre.src-scheme:before { content: 'Scheme'; }
  pre.src-screen:before { content: 'Gnu Screen'; }
  pre.src-sed:before { content: 'Sed'; }
  pre.src-sh:before { content: 'shell'; }
  pre.src-sql:before { content: 'SQL'; }
  pre.src-sqlite:before { content: 'SQLite'; }
  /* additional languages in org.el's org-babel-load-languages alist */
  pre.src-forth:before { content: 'Forth'; }
  pre.src-io:before { content: 'IO'; }
  pre.src-J:before { content: 'J'; }
  pre.src-makefile:before { content: 'Makefile'; }
  pre.src-maxima:before { content: 'Maxima'; }
  pre.src-perl:before { content: 'Perl'; }
  pre.src-picolisp:before { content: 'Pico Lisp'; }
  pre.src-scala:before { content: 'Scala'; }
  pre.src-shell:before { content: 'Shell Script'; }
  pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
  /* additional language identifiers per "defun org-babel-execute"
       in ob-*.el */
  pre.src-cpp:before  { content: 'C++'; }
  pre.src-abc:before  { content: 'ABC'; }
  pre.src-coq:before  { content: 'Coq'; }
  pre.src-groovy:before  { content: 'Groovy'; }
  /* additional language identifiers from org-babel-shell-names in
     ob-shell.el: ob-shell is the only babel language using a lambda to put
     the execution function name together. */
  pre.src-bash:before  { content: 'bash'; }
  pre.src-csh:before  { content: 'csh'; }
  pre.src-ash:before  { content: 'ash'; }
  pre.src-dash:before  { content: 'dash'; }
  pre.src-ksh:before  { content: 'ksh'; }
  pre.src-mksh:before  { content: 'mksh'; }
  pre.src-posh:before  { content: 'posh'; }
  /* Additional Emacs modes also supported by the LaTeX listings package */
  pre.src-ada:before { content: 'Ada'; }
  pre.src-asm:before { content: 'Assembler'; }
  pre.src-caml:before { content: 'Caml'; }
  pre.src-delphi:before { content: 'Delphi'; }
  pre.src-html:before { content: 'HTML'; }
  pre.src-idl:before { content: 'IDL'; }
  pre.src-mercury:before { content: 'Mercury'; }
  pre.src-metapost:before { content: 'MetaPost'; }
  pre.src-modula-2:before { content: 'Modula-2'; }
  pre.src-pascal:before { content: 'Pascal'; }
  pre.src-ps:before { content: 'PostScript'; }
  pre.src-prolog:before { content: 'Prolog'; }
  pre.src-simula:before { content: 'Simula'; }
  pre.src-tcl:before { content: 'tcl'; }
  pre.src-tex:before { content: 'TeX'; }
  pre.src-plain-tex:before { content: 'Plain TeX'; }
  pre.src-verilog:before { content: 'Verilog'; }
  pre.src-vhdl:before { content: 'VHDL'; }
  pre.src-xml:before { content: 'XML'; }
  pre.src-nxml:before { content: 'XML'; }
  /* add a generic configuration mode; LaTeX export needs an additional
     (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
  pre.src-conf:before { content: 'Configuration File'; }

  table { border-collapse:collapse; }
  caption.t-above { caption-side: top; }
  caption.t-bottom { caption-side: bottom; }
  td, th { vertical-align:top;  }
  th.org-right  { text-align: center;  }
  th.org-left   { text-align: center;   }
  th.org-center { text-align: center; }
  td.org-right  { text-align: right;  }
  td.org-left   { text-align: left;   }
  td.org-center { text-align: center; }
  dt { font-weight: bold; }
  .footpara { display: inline; }
  .footdef  { margin-bottom: 1em; }
  .figure { padding: 1em; }
  .figure p { text-align: center; }
  .inlinetask {
    padding: 10px;
    border: 2px solid gray;
    margin: 10px;
    background: #ffffcc;
  }
  #org-div-home-and-up
   { text-align: right; font-size: 70%; white-space: nowrap; }
  textarea { overflow-x: auto; }
  .linenr { font-size: smaller }
  .code-highlighted { background-color: #ffff00; }
  .org-info-js_info-navigation { border-style: none; }
  #org-info-js_console-label
    { font-size: 10px; font-weight: bold; white-space: nowrap; }
  .org-info-js_search-highlight
    { background-color: #ffff00; color: #000000; font-weight: bold; }
  .org-svg { width: 90%; }
  /*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
<script type="text/javascript">
/*
@licstart  The following is the entire license notice for the
JavaScript code in this tag.

Copyright (C) 2012-2017 Free Software Foundation, Inc.

The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.  The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.


@licend  The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
 function CodeHighlightOn(elem, id)
 {
   var target = document.getElementById(id);
   if(null != target) {
     elem.cacheClassElem = elem.className;
     elem.cacheClassTarget = target.className;
     target.className = "code-highlighted";
     elem.className   = "code-highlighted";
   }
 }
 function CodeHighlightOff(elem, id)
 {
   var target = document.getElementById(id);
   if(elem.cacheClassElem)
     elem.className = elem.cacheClassElem;
   if(elem.cacheClassTarget)
     target.className = elem.cacheClassTarget;
 }
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">Grep</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org2841edb">1. GREP</a>
<ul>
<li><a href="#org4d35845">1.1. Simple description of Grep:</a></li>
<li><a href="#org8adb0a3">1.2. Basic example</a></li>
<li><a href="#org16f31f9">1.3. Better examples</a>
<ul>
<li><a href="#org05243c7">1.3.1. Table for grep commands</a></li>
</ul>
</li>
<li><a href="#org79394db">1.4. What else can be done</a></li>
<li><a href="#orgd0821e5">1.5. Exercise</a></li>
<li><a href="#orgfb89fdf">1.6. Resources</a></li>
</ul>
</li>
</ul>
</div>
</div>

<div id="outline-container-org2841edb" class="outline-2">
<h2 id="org2841edb"><span class="section-number-2">1</span> GREP</h2>
<div class="outline-text-2" id="text-1">
<p>
<i>Dependencies:</i> In order to learn grep this can be necessary for you to learn before:
</p>
<ul class="org-ul">
<li>Unix Pipes</li>
<li>Shell Basics</li>
<li>Data Streams</li>
<li>Unix-like Operative Systems</li>
</ul>
</div>

<div id="outline-container-org4d35845" class="outline-3">
<h3 id="org4d35845"><span class="section-number-3">1.1</span> Simple description of Grep:</h3>
<div class="outline-text-3" id="text-1-1">
<p>
Grep is a UNIX command for searching text that matches a string.
If we want to look for "text" in $file then grep is exactly the tool we need! 
It can also be used with the UNIX pipeline <code>|</code>
</p>

<ul class="org-ul">
<li>Before we start learning grep please create the following file named <code>example_file.txt</code></li>
</ul>
<pre class="example">
example_file.txt
The Apple is red
The orange is old
The pear is tasty 
The date of the party
The dragon likes to eat fruit
I don't know what dewberry is
My berries are too old
for $FILE 
*bold*
\path\to\file
100.00
52.34
Up here^
[some stuff]
</pre>
</div>
</div>


<div id="outline-container-org8adb0a3" class="outline-3">
<h3 id="org8adb0a3"><span class="section-number-3">1.2</span> Basic example</h3>
<div class="outline-text-3" id="text-1-2">
<pre class="example">
$ grep orange example_file.txt
The orange is old
</pre>


<p>
That simple command is the way to use grep: You call grep with two arguments, first one is the text you want to find, 
second is where you want to find it. Of course that is very simple, so we should look at better examples.
</p>
</div>
</div>

<div id="outline-container-org16f31f9" class="outline-3">
<h3 id="org16f31f9"><span class="section-number-3">1.3</span> Better examples</h3>
<div class="outline-text-3" id="text-1-3">
<p>
Now that we have seen grep in action is time to put it to better use, after all grep is one of the most powerful and useful commands in the UNIX world. 
First of all you should notice that grep is <b>case sensitive.</b>  This means that if you call <code>$grep foo file.txt</code>  grep will only find  "foo" but not "Foo". 
</p>

<ul class="org-ul">
<li>Case insensitive search can be done with the flag <code>-i</code></li>
</ul>

<pre class="example">
$ grep -i apple example_file.txt
The Apple is red
</pre>

<p>
It is common in the UNIX world to have the need to look for meta characters  <code>^$\.*[]</code> 
</p>

<ul class="org-ul">
<li>Meta characters can be find with the special flag <code>-F</code></li>
</ul>

<pre class="example">
$ grep -F '$FILE' example_file.txt 
for $FILE
</pre>


<ul class="org-ul">
<li>Another common case might be the need for looking for two or more different strings. We can have different options</li>
</ul>
<p>
here:
</p>
<ul class="org-ul">
<li>Match any of the strings: use <code>-e</code> flag</li>
</ul>

<pre class="example">
$ grep -e 'foo' -e 'bar'
</pre>


<ul class="org-ul">
<li>Match all of the strings: use a pipeline <code>|</code> to stream from one grep to another</li>
</ul>

<pre class="example">
$ grep 'foo' | grep 'bar'
</pre>

<ul class="org-ul">
<li>Sometimes you are looking for a pattern for example, you have a bunch of names file01.txt file02.txt and so on, in this case a period <code>.</code> can be used as a wildcard that matches exactly one character</li>
</ul>

<pre class="example">
$ls
file01 file02 file03 file001
$ ls | grep file.1 
file01
</pre>

<p>
-Notice that <i>file001</i> was not matched, since <code>.</code> stands for only one character, if you need more characters just add more <code>.</code> one for each character
</p>

<pre class="example">
$ls
file01 file02 file03 file001
$ ls | grep file..1 
file001
</pre>

<p>
Now that we know how to use the widcard <code>.</code> you might be wondering what happens if you are looking for a file with a period like file.txt.
</p>

<ul class="org-ul">
<li>In this case the correct command is <code>grep 'file\.txt'</code> using both <code>'</code> and <code>\</code> , otherwise grep will match also file1txt file-txt fileatxt and so on:</li>
</ul>

<pre class="example">
$ls
file.txt file1txt file-txt fileatxt
$ ls | grep 'file\.txt '
file.txt
</pre>
</div>

<div id="outline-container-org05243c7" class="outline-4">
<h4 id="org05243c7"><span class="section-number-4">1.3.1</span> Table for grep commands</h4>
<div class="outline-text-4" id="text-1-3-1">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Metacharacter</th>
<th scope="col" class="org-left">Function</th>
<th scope="col" class="org-left">Example</th>
<th scope="col" class="org-left">Descrpition</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">^</td>
<td class="org-left">Beginning-of-line anchor</td>
<td class="org-left"><code>'^Up'</code></td>
<td class="org-left">Will display all lines beginning with <code>Up</code></td>
</tr>

<tr>
<td class="org-left">$</td>
<td class="org-left">End-of-line anchor</td>
<td class="org-left"><code>'old'$</code></td>
<td class="org-left">Will display all lines ending with <code>old</code></td>
</tr>

<tr>
<td class="org-left">.</td>
<td class="org-left">Matches single character</td>
<td class="org-left"><code>a..e</code></td>
<td class="org-left">Will display lines containing <code>a</code> followed by <i>two</i> characters, followed y an e</td>
</tr>

<tr>
<td class="org-left">*</td>
<td class="org-left">Matches zero or more characters preceding the asterisk</td>
<td class="org-left"><code>'too*'</code></td>
<td class="org-left">Will display lines with 'to', or 'too'  because 'to' is a zero match character and 'too' is a one match character</td>
</tr>

<tr>
<td class="org-left">[ ]</td>
<td class="org-left">Matches single character in the set</td>
<td class="org-left"><code>'[Aa]pple'</code></td>
<td class="org-left">Will display lines containing <code>Apple</code> or <code>apple</code></td>
</tr>

<tr>
<td class="org-left">[^]</td>
<td class="org-left">Matches single character not in the set</td>
<td class="org-left"><code>'[^Tt]he'</code></td>
<td class="org-left">Will display lines not containing a character <code>T</code> or <code>t</code> followed by <code>he</code> but it will display all the other lines with <code>he</code></td>
</tr>

<tr>
<td class="org-left">\&lt;</td>
<td class="org-left">Beginning-of-word anchor</td>
<td class="org-left"><code>'\&lt;date'</code></td>
<td class="org-left">Will display lines containing a word that begins with "date"</td>
</tr>

<tr>
<td class="org-left">\&gt;</td>
<td class="org-left">End-of-word anchor</td>
<td class="org-left"><code>'ear\&gt;'</code></td>
<td class="org-left">Will display lines containing a word that ends with "ear"</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>



<div id="outline-container-org79394db" class="outline-3">
<h3 id="org79394db"><span class="section-number-3">1.4</span> What else can be done</h3>
<div class="outline-text-3" id="text-1-4">
<p>
Cool stuff that you can do now with this new knowledge.
</p>


<p>
A pretty basic use of the pipeline with grep is explained next: 
</p>

<pre class="example">
$ grep -v "e$" example_file.txt | grep "^d"
</pre>

<p>
The first command <code>grep -v "e$" example_file.txt</code> matches all lines ending in <code>"e"</code>. The <code>"-v"</code> flag means <i>omit all 
matches,</i> thus the matching <code>"e$"</code> lines don't show up on stdout.
</p>

<p>
(<b>Note</b> In UNIX <code>$</code> represents the end of a line coversely <code>^</code> is the beginning of a line <b>End Note</b>)
</p>

<p>
The result of this command on stdout is (as said before, this won't be shown to user):
</p>
<pre class="example">
pear
dragonfruit
dewberry
berries
</pre>
<p>
Which is then piped into the second command, <code>grep "^d"</code>. Just like how having <code>"$"</code> next to <code>"e"</code> meant <i>match e
when it's next to the end of the line</i>, having <code>"^"</code> next to <code>"d"</code> meants <i>match d when it's next to the start of the line.</i>
</p>

<p>
So, the final ouput would be this:
</p>
<pre class="example">
dragonfruit
dewberry
</pre>
</div>
</div>

<div id="outline-container-orgd0821e5" class="outline-3">
<h3 id="orgd0821e5"><span class="section-number-3">1.5</span> Exercise</h3>
<div class="outline-text-3" id="text-1-5">
<p>
exercise.txt:
exercise.sh:
</p>

<pre class="example">
if $(whereis wget)
then
wget komprendo.net/x/x/exercise.txt
else
curl komprendo.net/x/x/exercise.txt &gt; exercise.txt
fi
</pre>
</div>
</div>


<div id="outline-container-orgfb89fdf" class="outline-3">
<h3 id="orgfb89fdf"><span class="section-number-3">1.6</span> Resources</h3>
<div class="outline-text-3" id="text-1-6">
<p>
info grep (better content than man grep)
</p>
<ul class="org-ul">
<li><a href="https://learnbyexample.gitbooks.io/command-line-text-processing/content/gnu_grep.html">Learn By Example - Grep</a></li>
<li><a href="http://www.panix.com/~elflord/unix/grep.html">Unix and Linux - Grep</a></li>
<li><a href="https://linuxjourney.com/lesson/grep-command">Linux Journey - Grep</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="date">Created: 2017-11-11 sáb 12:27</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>
